package com.junjun.ssm.servilce.servlceImpl;

import com.junjun.ssm.dao.INewByHqlDao;
import com.junjun.ssm.dao.INewsDao;
import com.junjun.ssm.entity.NewsEntity;
import com.junjun.ssm.entity.NewsEs;
import com.junjun.ssm.servilce.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {

    @Autowired
    INewsDao newsDao;

    @Autowired
    INewByHqlDao newByHqlDao;


    @Override
    public void add(String userid, String title, String author, String info, String createDate) {
        NewsEntity news = new NewsEntity();
        news.setNewsId(userid);
        news.setTitle(title);
        news.setAuthor(author);
        news.setNewsTime(createDate);
        newByHqlDao.save(news);

        NewsEs news1 = new NewsEs();
        news1.setId(userid);
        news1.setInfo(info);
        news1.setTitle(title);
        news1.setAuthor(author);
        news1.setCreateDate(createDate);
        newsDao.save(news1);


    }

    @Override
    public List<NewsEntity> selectAll(String queryInfo) {
        if (queryInfo!=null && !queryInfo.equals("")){
            List<NewsEs> newsEsList = newsDao.findAllByInfo(queryInfo);
            List<NewsEntity> list=new ArrayList<>();
            for (NewsEs newsEs : newsEsList) {
                list.add(newByHqlDao.queryAllById(newsEs.getId()));
            }
            return list;
        }else {
            return newByHqlDao.queryAll();
        }


    }

    @Override
    public NewsEs queryEsById(String id) {
        return newsDao.findAllById(id);
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        newsDao.deleteById(id);
        newByHqlDao.deleteById(id);
    }


}
